package org.dinky.utils;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import org.dinky.data.result.MyJdbcProperties;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyJdbcUtil {

    public static MyJdbcProperties parseHostAndPortByUrl(String url) {
        String regStr = "\\w+:\\w+://([\\w\\.]+):(\\d+)/?\\S+";
        Pattern pattern=Pattern.compile(regStr);
        // 3.创建匹配器//说明：创建匹配器matcher，按照正则表达式的规则去匹配content字符串
        Matcher matcher=pattern.matcher(url);
        String hostname = "";
        Integer port = 3306;
        if (matcher.find()){
            hostname = matcher.group(1);
            String tmpPort = matcher.group(2);
            if (NumberUtil.isNumber(tmpPort)){
                port = Integer.valueOf(tmpPort);
            }
        }
        MyJdbcProperties myJdbcProperties = new MyJdbcProperties();
        myJdbcProperties.setHostname(hostname);
        myJdbcProperties.setPort(port);
        return myJdbcProperties;

    }
}
